<?php
require_once 'application/constant/constant.php';
require_once 'application/library/helper/configHelper.php';

/**
 * csvHelper
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class  csvHelper{
    const COMMA_DELIMITER = ",";
    const TAB_DELIMITER = "\t";
    const ENCLOSURE = "\"";

    /**
     * 新建CSV文件
     * creatCsvFile
     * @param   array   $parameter
     * @param   string  $fileName
     * @return  bool
     */
    public static function creatCsvFile($parameter, $fileName, $filePath = null){
        if ($filePath == null){
            $file = fopen(configHelper::getGlobalVariable("tempFilePath") . Character::PATH_SEPARATOR . $fileName, "w");
        } else {
            $file = fopen($filePath . $fileName, "w");
        }
        if(!$file){
            return false;
        }
        foreach($parameter as $rowdata) {
            $SJrowdata =array();
            foreach($rowdata as $strParam => $rowdataparameter){
                $SJrowdata[$strParam] = iconv("utf-8", "utf-8", $rowdataparameter);
            }
          
            if(!fputcsv($file, $SJrowdata, self::COMMA_DELIMITER, self::ENCLOSURE)) {
                fclose($file);
                return false;
            }
        }
        if(!fclose($file)) {
            return false;
        }

        return true;
    }

    /**
     * 新规TSV文件
     * creatTsvFile
     * @param   array   $parameter
     * @param   string  $fileName
     * @return  bool
     */
    public static function creatTsvFile($parameter, $fileName, $filePath = null){
        if ($filePath == null){
            $file = fopen(configHelper::getGlobalVariable("tempFilePath"). Character::PATH_SEPARATOR . $fileName, "w");
        } else {
            $file = fopen($filePath . $fileName, "w");
        }
        if(!$file){
            return false;
        }
        foreach($parameter as $rowdata) {
            if(!fputcsv($file, $rowdata, self::TAB_DELIMITER, self::ENCLOSURE)) {
                fclose($file);
                return false;
            }
        }
        if(!fclose($file)) {
            return false;
        }
        return true;
    }

    /**
     * CSV文件内容取得
     * readCsvFile
     * @param   string  $fileName
     * @return  array, when error return false
     */
    public static function readCsvFile($fileName, $filePath = null){
        if ($filePath == null){
            $file = fopen(configHelper::getGlobalVariable("tempFilePath") . Character::PATH_SEPARATOR . $fileName, "r");
        } else {
            $file = fopen($filePath . $fileName, "r");
        }
        if(!$file){
            return false;
        }
        $arrReturn = array();
        while($rowdata = fgetcsv($file, 0, self::COMMA_DELIMITER, self::ENCLOSURE)) {
            $arrReturn[] = $rowdata;
        }
        if(!fclose($file)){
            return false;
        }
        return $arrReturn;
    }
    
    /**
     * TSV文件内容取得
     * readTsvFile
     * @param   string  $fileName
     * @return  array, when error return false
     */
    public static function readTsvFile($fileName, $filePath = null){
        if ($filePath == null){
            $file = fopen(configHelper::getGlobalVariable("tempFilePath") . Character::PATH_SEPARATOR . $fileName, "r");
        } else {
            $file = fopen($filePath . $fileName, "r");
        }
        if(!$file){
            return false;
        }
        $arrReturn = array();
        while($rowdata = fgetcsv($file, 0, self::TAB_DELIMITER, self::ENCLOSURE)) {
            $arrReturn[] = $rowdata;
        }
        if(!fclose($file)){
            return false;
        }
        return $arrReturn;
    }
}
